package com.hotel.common.user.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.generator.config.IFileCreate;
import com.hotel.common.config.RabbitKeyConfig;
import com.hotel.common.param.MqMsg;
import com.hotel.common.vo.R;
import com.hotel.common.user.entity.SmsLog;
import com.hotel.common.user.entity.User;
import com.hotel.common.user.mapper.SmsLogMapper;
import com.hotel.common.user.mapper.UserMapper;
import com.hotel.common.user.service.SmsLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * <p>
 * 验证码日志表 服务实现类
 * </p>
 *
 * @author mz
 * @since 2024-06-15
 */
@Service
public class SmsLogServiceImpl extends ServiceImpl<SmsLogMapper, SmsLog> implements SmsLogService {


    @Autowired
    private SmsLogMapper smsLogMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

/**
 *
 * 1校验手机号是否为空
 * 2.校验手机号 是否被其他用户使用
 * 3.发送手机验证码
 * 4.返回数据 验证已发送，请查收
 * @param phone
 * @return
 */

@Override
public R sendrcode(String phone){

    //  1校验手机号是否为空
    if (StringUtils.isEmpty(phone)){
        return R.fail("手机号为空");
    }

    // 2.查询号码是否被注册
    QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
    userQueryWrapper.eq("phone",phone);
    List<User> userList = userMapper.selectList(userQueryWrapper);
    if (userList!=null && userList.size()>0){
        return R.fail("手机号已经被注册");
    }

    // * 3.发送手机验证码
    rabbitTemplate.convertAndSend("", RabbitKeyConfig.Q_SENDSMS,new MqMsg(IdUtil.getSnowflakeNextId(),1,phone));


    // 4. 返回数据  验证码已发送
    return R.ok("验证码已经发送");

}

    /**
     * 手机号  + 验证码 登录
     * @param phone
     * @return
     */
    @Override
    public R logincode(String phone) {
    if (StringUtils.isEmpty(phone)){
        return R.fail("手机号为空");
    }
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
    userQueryWrapper.eq("phone",phone);
        List<User> userList = userMapper.selectList(userQueryWrapper);
        if (userList == null || userList.size() == 0){
            return R.fail("该手机号未被注册");
        }
        rabbitTemplate.convertAndSend("",RabbitKeyConfig.Q_SENDSMS,new MqMsg(IdUtil.getSnowflakeNextId(),2,phone));
        return R.ok("验证码已经发送");
    }


    // 忘记密码
    @Override
    public R find(String phone) {

        if (StringUtils.isEmpty(phone)){
            return R.fail("手机号为空");
        }

        // 查询数据库中是否有匹配的已注册手机号
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("phone",phone);
        User selectOne = userMapper.selectOne(userQueryWrapper);
        // 检查查询结果，确保手机号已注册
        if (selectOne == null){
            return R.fail("该手机号未被注册");
        }

        // 确认手机号匹配已注册用户后，发送验证码
        rabbitTemplate.convertAndSend("",RabbitKeyConfig.Q_SENDSMS,new MqMsg(IdUtil.getSnowflakeNextId(),3,phone));

        return R.ok("验证码已经发送");
    }


}
